package com.dha.asynctask;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.dha.adapter.XemDiemAdapter;
import com.dha.caodangcongnghe.R;
import com.dha.model.DiemModel;
import com.dha.variable.ThongTinTaiKhoan;
import com.dha.variable.XemDiemVariable;

public class SinhVienXemDiemAsyncTask extends AsyncTask<Void, String, Integer> {
	private Activity activity;
	private ProgressDialog dialog;
	private XemDiemAdapter adapter;
	private ArrayList<DiemModel> arr;
	private String maSinhVien;
	private ListView lvBangDiem;
	private String meThodName = null;
	//
	private final int CHUA_CO_DIEM = 101;
	private final int OK = 100;
	private final int LOI_LAY_DIEM = 102;

	public SinhVienXemDiemAsyncTask(Activity activity, String maSinhVien,
			String methodName) {
		super();
		this.activity = activity;
		arr = new ArrayList<DiemModel>();
		this.maSinhVien = maSinhVien;
		this.meThodName = methodName;

		lvBangDiem = (ListView) activity.findViewById(R.id.lv_DS_Diem);
		adapter = new XemDiemAdapter(activity,
				R.layout.layout_xemdiem_custom_table, arr);
		lvBangDiem.setAdapter(adapter);

	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang tải bảng điểm về");
		super.onPreExecute();
	}

	// Khuc nay
	/*
	 * Nếu là 1 cái, thì chạy ra trắng Từ 2 cái trở lên, mới chạy Neu 2 hoac
	 * nhieu chua co diem ==> trắng Nếu 2 cái, 1 cái có điểm, 1 cái không ==>
	 * hiện cái có điểm
	 */
	@Override
	protected Integer doInBackground(Void... params) {
		SoapObject request;
		PropertyInfo maSinhVienInfo, maHocKyInfo;
		SoapSerializationEnvelope envelope = null;
		try {
			request = new SoapObject(XemDiemVariable.NAME_SPACE, meThodName);

			maSinhVienInfo = new PropertyInfo();
			maSinhVienInfo.setName("MaSinhVien");
			maSinhVienInfo.setValue(maSinhVien);
			request.addProperty(maSinhVienInfo);

			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTransportSE = new HttpTransportSE(
					XemDiemVariable.URL);
			httpTransportSE.call(XemDiemVariable.NAME_SPACE + meThodName,
					envelope);

			// Neu khong phai la 1 mon thi chay cai nay
			Vector<SoapObject> respone = (Vector<SoapObject>) envelope
					.getResponse();
			if (respone != null) {
				for (SoapObject so : respone) {
					publishProgress(so.getProperty("diemChu").toString(), so
							.getProperty("diemTongKet").toString(), so
							.getProperty("hocKy").toString(),
							so.getProperty("maHocPhan").toString(), so
									.getProperty("maLopHocPhan").toString(), so
									.getProperty("soTinChi").toString(), so
									.getProperty("tenHocPhan").toString(), so
									.getProperty("tichLuy").toString());
				}
				return OK;
			} else {
				return LOI_LAY_DIEM;
			}
		} catch (java.lang.ClassCastException ex) {
			// neu la 1 mon co diem, no cast sai, ra exceop ti
			// ==> vao day xu ly
			try {
				SoapObject so = (SoapObject) envelope.getResponse();
				// 1 mon, tra ve so luong proper ti, neu chua co diem thi diem
				// tong ket va diem chu null
				// ==> co 6 thanh phan, in ra trang, neu la 8 co 2 property tren
				// thi in ra bang diem
				if (so != null) {
					if (so.getPropertyCount() == 8) {

						publishProgress(so.getProperty("diemChu").toString(),
								so.getProperty("diemTongKet").toString(), so
										.getProperty("hocKy").toString(), so
										.getProperty("maHocPhan").toString(),
								so.getProperty("maLopHocPhan").toString(), so
										.getProperty("soTinChi").toString(), so
										.getProperty("tenHocPhan").toString(),
								so.getProperty("tichLuy").toString());
						return OK;
					} else {
						return CHUA_CO_DIEM;
					}
				} else {
					return LOI_LAY_DIEM;
				}
				// for(int i = 0; i< respone.getPropertyCount(); i++){
				// publishProgress(respone.getProperty(i).toString());
				// }

			} catch (SoapFault e) {
				
				// TODO Auto-generated catch block
				return LOI_LAY_DIEM;
				
			}

		} catch (Exception e) {
			return CHUA_CO_DIEM;
		}
	}

	@Override
	protected void onProgressUpdate(String... values) {
		String diemChu = values[0].toString();
		String diemTongKet = values[1].toString();
		String hocKy = values[2].toString();
		String maHocPhan = values[3].toString();
		String maLopHocPhan = values[4].toString();
		String soTinChi = values[5].toString();
		String tenHocPhan = values[6].toString();
		String tichLuy = values[7].toString();
		Boolean isTichLuy = null;
		if (tichLuy.equals("1")) {
			isTichLuy = true;
		} else {
			isTichLuy = false;
		}

		DiemModel diem = new DiemModel(tenHocPhan, maHocPhan, maLopHocPhan,
				soTinChi, diemTongKet, diemChu, null, hocKy, isTichLuy);

		arr.add(diem);

		// adapter.notifyDataSetChanged();

		super.onProgressUpdate(values);
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);

		AlertDialog.Builder builder = null;
		builder = new Builder(activity);
		builder.setNegativeButton("Đóng", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
//				activity.finish();
			}
		});
		AlertDialog alertDialog = null;
		switch (result) {
		case CHUA_CO_DIEM:
			Toast.makeText(activity, "Chưa có điểm đầy đủ", Toast.LENGTH_LONG).show();
			break;
		case LOI_LAY_DIEM: 
			builder.setTitle("Lỗi");
			builder.setMessage("Lỗi trong việc lấy điểm");
			alertDialog = builder.create();
			alertDialog.show();
			break;
		}

		adapter.notifyDataSetChanged();
		dialog.dismiss();
	}

}
